package cn.tedu.serviceEdu.controller;


import cn.tedu.commonUtils.R;
import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.serviceEdu.client.VodClient;
import cn.tedu.serviceEdu.entity.EduVideo;
import cn.tedu.serviceEdu.service.EduChapterService;
import cn.tedu.serviceEdu.service.EduCourseService;
import cn.tedu.serviceEdu.service.EduVideoService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;
    @Resource
    private VodClient vodClient;

    @PostMapping("/saveVideo")
    public R saveVideo(@RequestBody EduVideo video) {
        if (video == null) {
            throw new EducationException(20001, "视屏不存在");
        }
        boolean save = videoService.save(video);
        if (!save) {
            throw new EducationException(20001, "视屏添加失败");
        }
        return R.ok();
    }


    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id) {
        //查询云端视频id

        EduVideo video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        System.out.println("removeVideoById:videoId="+videoSourceId);
        //删除视频资源
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeVideo(videoSourceId);
        }

        videoService.removeById(id);
        return R.ok();
    }

    @GetMapping("/getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id) {
        if (id.isEmpty()) {
            throw new EducationException(20001, "视屏不存在");
        }
        EduVideo video = videoService.getById(id);
        return R.ok().data("items", video);
    }

    @PutMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo video) {
        if (video == null) {
            throw new EducationException(20001, "视屏不存在");
        }
        videoService.updateById(video);
        return R.ok();
    }
}


